package com.aaa.realm;

import com.aaa.entity.UserLogin;
import com.aaa.service.UserLoginService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class MyShiroRealm extends AuthorizingRealm {

    @Autowired
    UserLoginService service;


    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取查询成功的对象
        String username = (String) authenticationToken.getPrincipal();
        UserLogin login = service.Login(username);
        // 将数据整理 返回 给 sm 安全管理器
        // Object principal 账号/login对象 （存什么 将来取出来的就是什么）  ,
        // Object hashedCredentials   数据库的密码   ,
        // ByteSource credentialsSalt, 盐值
        // String realmName   当前realmName
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(login , login.getPassword() , ByteSource.Util.bytes(login.getSalt()), getName());

        return simpleAuthenticationInfo;
    }
}
